<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>php-amqplib库操作RabbitMQ - 古一 - 个人博客</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="古一" /><meta name="description" content="1. composer安装php-amqplib库 composer require php-amqplib/php-amqplib 2. 生产者代码 send.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 &amp;lt;?php require_once __DIR__.&amp;#39;/vendor/autoload.php&amp;#39;; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPStreamConnection(&amp;#39;172.17.0.2&amp;#39;, 5672, &amp;#39;admin&amp;#39;, &amp;#39;admin&amp;#39;); $channel = $connection-&amp;gt;channel();" /><meta name="keywords" content="古一, 博客, 网站" />






<meta name="generator" content="Hugo 0.81.0 with theme even" />


<link rel="canonical" href="https://guyi2020.github.io/blog/post/php-amqplib%E5%BA%93%E6%93%8D%E4%BD%9Crabbitmq/" />
<link rel="apple-touch-icon" sizes="180x180" href="/blog/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/blog/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/blog/favicon-16x16.png">
<link rel="manifest" href="/blog/manifest.json">
<link rel="mask-icon" href="/blog/safari-pinned-tab.svg" color="#5bbad5">



<link href="/blog/sass/main.min.af7fd1da18d66c2b017df5b4cae508ef44cfcac3fb4c7c7a327fe4f4f9e28b08.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="php-amqplib库操作RabbitMQ" />
<meta property="og:description" content="1. composer安装php-amqplib库 composer require php-amqplib/php-amqplib 2. 生产者代码 send.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 &lt;?php require_once __DIR__.&#39;/vendor/autoload.php&#39;; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPStreamConnection(&#39;172.17.0.2&#39;, 5672, &#39;admin&#39;, &#39;admin&#39;); $channel = $connection-&gt;channel();" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://guyi2020.github.io/blog/post/php-amqplib%E5%BA%93%E6%93%8D%E4%BD%9Crabbitmq/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2021-05-27T11:11:00&#43;08:00" />
<meta property="article:modified_time" content="2021-05-28T11:12:00&#43;08:00" />

<meta itemprop="name" content="php-amqplib库操作RabbitMQ">
<meta itemprop="description" content="1. composer安装php-amqplib库 composer require php-amqplib/php-amqplib 2. 生产者代码 send.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 &lt;?php require_once __DIR__.&#39;/vendor/autoload.php&#39;; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPStreamConnection(&#39;172.17.0.2&#39;, 5672, &#39;admin&#39;, &#39;admin&#39;); $channel = $connection-&gt;channel();"><meta itemprop="datePublished" content="2021-05-27T11:11:00&#43;08:00" />
<meta itemprop="dateModified" content="2021-05-28T11:12:00&#43;08:00" />
<meta itemprop="wordCount" content="180">
<meta itemprop="keywords" content="PHP,RabbitMQ," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="php-amqplib库操作RabbitMQ"/>
<meta name="twitter:description" content="1. composer安装php-amqplib库 composer require php-amqplib/php-amqplib 2. 生产者代码 send.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 &lt;?php require_once __DIR__.&#39;/vendor/autoload.php&#39;; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPStreamConnection(&#39;172.17.0.2&#39;, 5672, &#39;admin&#39;, &#39;admin&#39;); $channel = $connection-&gt;channel();"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/blog/" class="logo">古一</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/blog/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/blog/post/">
        <li class="mobile-menu-item">文章</li>
      </a><a href="/blog/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/blog/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/blog/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/blog/" class="logo">古一</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/blog/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/blog/post/">文章</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/blog/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/blog/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/blog/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">php-amqplib库操作RabbitMQ</h1>

      <div class="post-meta">
        <span class="post-time"> 2021-05-27 </span>
        <div class="post-category">
            <a href="/blog/categories/%E6%8A%80%E6%9C%AF/"> 技术 </a>
            </div>
          <span class="more-meta"> 约 180 字 </span>
          <span class="more-meta"> 预计阅读 1 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#1-composer安装php-amqplib库">1. composer安装php-amqplib库</a></li>
        <li><a href="#2-生产者代码">2. 生产者代码</a></li>
        <li><a href="#3-消费者代码">3. 消费者代码</a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h2 id="1-composer安装php-amqplib库">1. composer安装php-amqplib库</h2>
<p><code>composer require php-amqplib/php-amqplib</code></p>
<h2 id="2-生产者代码">2. 生产者代码</h2>
<blockquote>
<p><strong>send.php</strong></p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-php" data-lang="php"><span class="o">&lt;?</span><span class="nx">php</span>

<span class="k">require_once</span> <span class="no">__DIR__</span><span class="o">.</span><span class="s1">&#39;/vendor/autoload.php&#39;</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">PhpAmqpLib\Connection\AMQPStreamConnection</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PhpAmqpLib\Message\AMQPMessage</span><span class="p">;</span>

<span class="nv">$connection</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">AMQPStreamConnection</span><span class="p">(</span><span class="s1">&#39;172.17.0.2&#39;</span><span class="p">,</span> <span class="mi">5672</span><span class="p">,</span> <span class="s1">&#39;admin&#39;</span><span class="p">,</span> <span class="s1">&#39;admin&#39;</span><span class="p">);</span>
<span class="nv">$channel</span> <span class="o">=</span> <span class="nv">$connection</span><span class="o">-&gt;</span><span class="na">channel</span><span class="p">();</span>

<span class="nv">$channel</span><span class="o">-&gt;</span><span class="na">queue_declare</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">,</span> <span class="k">false</span><span class="p">,</span> <span class="k">false</span><span class="p">,</span> <span class="k">false</span><span class="p">,</span> <span class="k">false</span><span class="p">);</span>

<span class="nv">$msg</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">AMQPMessage</span><span class="p">(</span><span class="s1">&#39;hello world!&#39;</span><span class="p">);</span>

<span class="nv">$channel</span><span class="o">-&gt;</span><span class="na">basic_publish</span><span class="p">(</span><span class="nv">$msg</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;hello&#39;</span><span class="p">);</span>

<span class="k">echo</span> <span class="s2">&#34; [x] Sent &#39;Hello World!&#39;</span><span class="se">\n</span><span class="s2">&#34;</span><span class="p">;</span>

<span class="nv">$channel</span><span class="o">-&gt;</span><span class="na">close</span><span class="p">();</span>
<span class="nv">$connection</span><span class="o">-&gt;</span><span class="na">close</span><span class="p">();</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="3-消费者代码">3. 消费者代码</h2>
<blockquote>
<p><strong>receive.php</strong></p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-php" data-lang="php"><span class="o">&lt;?</span><span class="nx">php</span>

<span class="k">require_once</span> <span class="no">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/vendor/autoload.php&#39;</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">PhpAmqpLib\Connection\AMQPStreamConnection</span><span class="p">;</span>

<span class="nv">$connection</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">AMQPStreamConnection</span><span class="p">(</span><span class="s1">&#39;172.17.0.2&#39;</span><span class="p">,</span> <span class="mi">5672</span><span class="p">,</span> <span class="s1">&#39;admin&#39;</span><span class="p">,</span> <span class="s1">&#39;admin&#39;</span><span class="p">);</span>
<span class="nv">$channel</span> <span class="o">=</span> <span class="nv">$connection</span><span class="o">-&gt;</span><span class="na">channel</span><span class="p">();</span>

<span class="nv">$channel</span><span class="o">-&gt;</span><span class="na">queue_declare</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">,</span> <span class="k">false</span><span class="p">,</span> <span class="k">false</span><span class="p">,</span> <span class="k">false</span><span class="p">,</span> <span class="k">false</span><span class="p">);</span>

<span class="k">echo</span> <span class="s2">&#34; [*] Waiting for messages. To exit press CTRL+C</span><span class="se">\n</span><span class="s2">&#34;</span><span class="p">;</span>

<span class="nv">$callback</span> <span class="o">=</span> <span class="k">function</span><span class="p">(</span><span class="nv">$msg</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">echo</span> <span class="s1">&#39; [x] Received &#39;</span><span class="p">,</span> <span class="nv">$msg</span><span class="o">-&gt;</span><span class="na">body</span><span class="p">,</span> <span class="s2">&#34;</span><span class="se">\n</span><span class="s2">&#34;</span><span class="p">;</span>
<span class="p">};</span>

<span class="nv">$channel</span><span class="o">-&gt;</span><span class="na">basic_consume</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="k">false</span><span class="p">,</span> <span class="k">true</span><span class="p">,</span> <span class="k">false</span><span class="p">,</span> <span class="k">false</span><span class="p">,</span> <span class="nv">$callback</span><span class="p">);</span>

<span class="k">while</span><span class="p">(</span><span class="nv">$channel</span><span class="o">-&gt;</span><span class="na">is_open</span><span class="p">())</span> <span class="p">{</span>
    <span class="nv">$channel</span><span class="o">-&gt;</span><span class="na">wait</span><span class="p">();</span>
<span class="p">}</span>

<span class="nv">$channel</span><span class="o">-&gt;</span><span class="na">close</span><span class="p">();</span>
<span class="nv">$connection</span><span class="o">-&gt;</span><span class="na">close</span><span class="p">();</span>
</code></pre></td></tr></table>
</div>
</div>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">古一</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2021-05-28
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content">MIT</span>
  </p>
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/blog/tags/php/">PHP</a>
          <a href="/blog/tags/rabbitmq/">RabbitMQ</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/blog/post/nodejs%E5%A4%9A%E7%89%88%E6%9C%AC/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">Nodejs多版本</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/blog/post/php%E9%A2%98%E7%9B%AE/">
            <span class="next-text nav-default">PHP题目</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
  <a href="https://guyi2020.github.io/blog/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2018 - 
    2021<span class="heart"><i class="iconfont icon-heart"></i></span><span>古一</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/blog/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        }
    };
  </script>
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.5/es5/tex-mml-chtml.js" integrity="sha256-HGLuEfFcsUJGhvB8cQ8nr0gai9EucOOaIxFw7qxmd+w=" crossorigin="anonymous"></script>








</body>
</html>
